<template>
  <div class="seller-sidebar">
    <div class="sidebar-logo">
      <img src="/MOMOR.png" alt="MOMOR" class="logo-image" />
    </div>

    <div class="sidebar-header">
      <h2>商家后台</h2>
      <div class="user-info" v-if="shopName" @click="handleUserInfoClick" :class="{ active: activeItem === 'info' }">
        <div class="user-avatar">
          <img v-if="logoUrl" :src="logoUrl" alt="商家Logo" class="shop-logo" />
          <i v-else class="avatar-icon">🏪</i>
        </div>
        <div class="user-details">
          <span class="username">{{ shopName }}</span>
          <span class="user-role">商家</span>
        </div>
        <i class="chevron-icon">›</i>
      </div>
    </div>

    <nav class="sidebar-nav">
      <ul class="nav-list">
          <li class="nav-item" :class="{ active: activeItem === 'analytics' }" @click="handleNavClick('analytics')">
          <div class="nav-link">
            <i class="icon">📊</i>
            <span>销售分析</span>
          </div>
        </li>
        
        <li class="nav-item" :class="{ active: activeItem === 'product' }" @click="handleNavClick('product')">
          <div class="nav-link">
            <i class="icon">📦</i>
            <span>商品管理</span>
          </div>
        </li>

        <li class="nav-item" :class="{ active: activeItem === 'order' }" @click="handleNavClick('order')">
          <div class="nav-link">
            <i class="icon">📋</i>
            <span>订单管理</span>
          </div>
        </li>

        <li class="nav-item" :class="{ active: activeItem === 'category' }" @click="handleNavClick('category')">
          <div class="nav-link">
            <i class="icon">🏷️</i>
            <span>分类管理</span>
          </div>
        </li>

      
      </ul>
    </nav>

    <div class="sidebar-footer">
      <div class="logout-btn" @click="handleLogout">
        <i class="icon">🚪</i>
        <span>登出</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SellerSidebar',
  props: {
    activeItem: {
      type: String,
      default: 'product'
    }
  },
  data() {
    return {
      shopName: '',
      logoUrl: ''
    }
  },
  mounted() {
    // 获取商家信息
    this.shopName = localStorage.getItem('shopName') || '';
    this.logoUrl = localStorage.getItem('logoUrl') || '';
  },
  methods: {
    handleNavClick(item) {
      this.$emit('nav-change', item);

      // 根据导航项跳转到对应页面
      switch(item) {
        case 'product':
          this.$router.push('/seller/product-manage');
          break;
        case 'order':
          this.$router.push('/seller/order-manage');
          break;
        case 'category':
          this.$router.push('/seller/category-manage');
          break;
        case 'analytics':
          this.$router.push('/seller/analytics');
          break;
        default:
          break;
      }
    },

    handleUserInfoClick() {
      this.$emit('nav-change', 'info');
      this.$router.push('/seller/info');
    },

    handleLogout() {
      // 清除本地存储的商家登录信息
      localStorage.removeItem('sellerToken');
      localStorage.removeItem('sellerInfo');
      localStorage.removeItem('sellerId');
      localStorage.removeItem('shopName');
      localStorage.removeItem('logoUrl');

      // 跳转到商家登录页面
      this.$router.push('/seller-login').catch(() => {
        // 如果路由跳转失败，则刷新页面
        window.location.reload();
      });
    }
  }
}
</script>

<style scoped>
.seller-sidebar {
  width: 250px;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  display: flex;
  flex-direction: column;
  box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
  flex-shrink: 0;
}

.sidebar-logo {
  padding: 20px;
  text-align: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.logo-image {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease;
}

.logo-image:hover {
  transform: scale(1.05);
  border-color: rgba(255, 255, 255, 0.4);
}

.sidebar-header {
  padding: 20px 20px 30px 20px;
  text-align: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-header h2 {
  margin: 0 0 20px 0;
  font-size: 24px;
  font-weight: 600;
  color: white;
}

.user-info {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: rgba(255, 255, 255, 0.1);
  padding: 12px 16px;
  border-radius: 10px;
  margin-top: 15px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.user-info:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateX(5px);
}

.user-info.active {
  background: rgba(255, 255, 255, 0.3);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.user-avatar {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.avatar-icon {
  font-size: 20px;
  color: white;
}

.shop-logo {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
}

.user-details {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  min-width: 0;
  flex: 1;
}

.chevron-icon {
  font-size: 16px;
  color: rgba(255, 255, 255, 0.7);
  transition: transform 0.3s ease;
}

.user-info:hover .chevron-icon {
  transform: translateX(3px);
}

.username {
  font-size: 16px;
  font-weight: 600;
  color: white;
  margin-bottom: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 120px;
}

.user-role {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.8);
  font-weight: 400;
}

.sidebar-nav {
  flex: 1;
  padding: 20px 0;
}

.nav-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.nav-item {
  margin: 5px 15px;
  border-radius: 10px;
  transition: all 0.3s ease;
  cursor: pointer;
}

.nav-item:hover {
  background: rgba(255, 255, 255, 0.1);
  transform: translateX(5px);
}

.nav-item.active {
  background: rgba(255, 255, 255, 0.2);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.nav-link {
  display: flex;
  align-items: center;
  padding: 15px 20px;
  text-decoration: none;
  color: white;
  font-size: 16px;
  font-weight: 500;
}

.icon {
  font-size: 20px;
  margin-right: 15px;
  width: 24px;
  text-align: center;
}

.sidebar-footer {
  padding: 20px 15px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

.logout-btn {
  display: flex;
  align-items: center;
  padding: 15px 20px;
  margin: 0;
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.3s ease;
  background: rgba(255, 255, 255, 0.1);
  color: white;
  font-size: 16px;
  font-weight: 500;
}

.logout-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: translateX(5px);
}

.logout-btn .icon {
  font-size: 20px;
  margin-right: 15px;
  width: 24px;
  text-align: center;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .seller-sidebar {
    width: 200px;
  }

  .sidebar-logo {
    padding: 15px;
  }

  .logo-image {
    width: 60px;
    height: 60px;
  }

  .sidebar-header h2 {
    font-size: 20px;
  }

  .user-info {
    padding: 10px 12px;
    gap: 10px;
  }

  .user-avatar {
    width: 35px;
    height: 35px;
  }

  .avatar-icon {
    font-size: 18px;
  }

  .username {
    font-size: 14px;
    max-width: 100px;
  }

  .user-role {
    font-size: 11px;
  }

  .nav-link, .logout-btn {
    font-size: 14px;
    padding: 12px 15px;
  }

  .icon {
    font-size: 18px;
    margin-right: 10px;
  }
}
</style>
